<template>
<div>
  <div
    :key="index"
    v-for="(file,index) in newFileList"
    >
    <span style="margin-right:30px">
     {{getFileName(file.name)}} 
     </span>
    (<el-button @click="handleDownLoad(file)" type="text">下载</el-button>
    |<el-button @click="handlePreview(file)"  type="text">预览</el-button>)
    </div>
    <el-dialog
        title="图片预览"
        :visible.sync="picDialogVisible"
        :show-close="false"
        width="50%">
        <img :src="'api/commonservice/obsfile/downloadfile?objectkey=' + picUrl" alt="" width="100%">
        <span slot="footer" class="dialog-footer">
            <el-button @click="picDialogVisible=false">取消</el-button>
        </span>
    </el-dialog>
</div>
</template>
<script>
import axios from 'axios'
import { fileToByteArray } from "common/src/api/culture/change";
import { downTemplate } from "common/src/api/upload";

export default {
    data() {
        return {
            picDialogVisible: false,
            picUrl: '',
            isScore:this.isScore
             
        }
    },
    props: {
        newFileList: {
            type: Array,
            default: []
        },
        isScore:{
            type:Boolean,
            default:false
        }
    },
    methods: {
        async handlePreview(file) {
        let _this = this
        if(file && file.url && file.name) {
            if((file.name).includes('.jpg') || (file.name).includes('.png') || (file.name).includes('.bmp')) {
                this.picUrl = file.url
                this.picDialogVisible = true
            } else if((file.name).includes('.rar') || (file.name).includes('.zip')) {
                this.$message.error("该类型文件暂不支持预览!")
            } else {
                let previewfile = {
                    url: file.url,
                    name: file.name
                    }
                const resp = await fileToByteArray(previewfile)
                _this.loading = true
                if (resp.code === 200) {
                    let data = {
                        originalFileName: resp.data.originalFileName,
                        content: resp.data.content
                    }
                    // 文件从obs转换到预览服务过程2：预览服务接收文件字节数组
                    const response = await axios.post(window.location.origin + '/filePre/fileImport', data, {
                        headers: { 'X-Token': localStorage.getItem('sessionid') || '' }
                    })
                    let path=this.isScore? resp.data.originalFileName.split('/')[2]:resp.data.originalFileName
                    if (response.data.code === 200) {
                        let lspdfUrl =
                        window.location.origin + '/filePre/onlinePreview?url=' +
                        window.location.origin + '/filePre/demo/' + path
                        window.open(lspdfUrl)
                    }  
                }
            }
        } else {
            this.$message.error("文件损坏!")
        }
      },

    handleDownLoad(file) {
        if(file && file.url && file.name) {
        let url = '/api/commonservice/obsfile/downloadfile?objectkey=' + file.url  
        let name = file.name
        downTemplate(name, url);
        } else {
            this.$message.error("文件损坏!")
        }
      },

    getFileName(path) {
        if (path.indexOf("/") != -1) {
          return path.substring(path.lastIndexOf("/") + 1);
        } else {
          return path;
        }
      }
    }
}
</script>
<style scoped lang="scss">
 
</style>